package org.gtug.vg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.gtug.vg.entities.NewEntity;

import java.util.ArrayList;
import java.util.List;


public class ListActivity extends Activity {
	private List<NewEntity> news = getNewsStub();
	private static final int CONTEXT_MENU_DELETE = 0;
	private static final String TAG = "ListActivity";
	ListView lv;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		ArrayAdapter<NewEntity> adapter = new ArrayAdapter<NewEntity>(this, android.R.layout.simple_list_item_1, getNewsStub());
		lv = (ListView) findViewById(R.id.listView);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				Intent intent = new Intent(ListActivity.this, NewDetailActivity.class);
				ArrayAdapter<NewEntity> adapter = (ArrayAdapter<NewEntity>) lv.getAdapter();
				NewEntity newEntity = adapter.getItem(i);
				intent.putExtra("new", newEntity);
				ListActivity.this.startActivity(intent);
			}
		});
		registerForContextMenu(lv);
	}

	private static String lorelIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
		"Praesent ut metus dignissim velit mattis condimentum. Nam congue bibendum facilisis. " +
		"Morbi feugiat mattis elit, et egestas sapien auctor blandit. Praesent aliquam, " +
		"ante sed hendrerit iaculis, felis velit fringilla purus, in sollicitudin nunc arcu ut dui.";

	private List<NewEntity> getNewsStub() {
		List<NewEntity> entities = new ArrayList<NewEntity>();
		for (int i = 0; i < 25; i++) {
			entities.add(new NewEntity("New Title " + i, lorelIpsum));
		}
		return entities;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterView.AdapterContextMenuInfo info;
		try {
			info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		} catch (ClassCastException e) {
			Log.e(TAG, "bad menuInfo", e);
			return;
		}
		NewEntity newEntity = (NewEntity) lv.getAdapter().getItem(info.position);
		menu.setHeaderTitle(newEntity.getNewTitle());
		menu.add(0, CONTEXT_MENU_DELETE, 0, "Delete");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
			case CONTEXT_MENU_DELETE: {
				ArrayAdapter<NewEntity> adapter = (ArrayAdapter<NewEntity>) lv.getAdapter();
				NewEntity newEntity = adapter.getItem(info.position);
				adapter.remove(newEntity);
				adapter.notifyDataSetChanged();
				Toast.makeText(ListActivity.this, newEntity.getNewTitle() + " Removed!!", Toast.LENGTH_SHORT).show();
				return true;
			}
		}
		return false;
	}
}